import tkinter as tk

# 定义计算器类
class Calculator:
    def __init__(self, root):
        self.root = root
        self.root.title("计算器")  # 设置窗口标题
        self.root.geometry("300x400")  # 设置窗口大小

        # 初始化变量
        self.current_input = ""  # 当前输入的内容
        self.result = 0  # 计算结果
        self.operator = None  # 当前运算符

        # 创建显示区域
        self.display = tk.Entry(root, width=20, font=("Arial", 20), justify="right", bd=10, relief="ridge")
        self.display.grid(row=0, column=0, columnspan=4)

        # 创建按钮布局
        buttons = [
            ("AC", 1, 0), ("+/-", 1, 1), ("%", 1, 2), ("/", 1, 3),
            ("7", 2, 0), ("8", 2, 1), ("9", 2, 2), ("*", 2, 3),
            ("4", 3, 0), ("5", 3, 1), ("6", 3, 2), ("-", 3, 3),
            ("1", 4, 0), ("2", 4, 1), ("3", 4, 2), ("+", 4, 3),
            ("0", 5, 0), (".", 5, 1), ("=", 5, 2)
        ]

        # 创建按钮并绑定事件
        for (text, row, col) in buttons:
            button = tk.Button(root, text=text, font=("Arial", 16), width=5, height=2,
                               command=lambda t=text: self.on_button_click(t))
            button.grid(row=row, column=col, padx=5, pady=5)
            if text in ["AC", "+/-", "%"]:  # 特殊按钮颜色
                button.config(bg="lightgray")
            elif text in ["/", "*", "-", "+", "="]:  # 运算符按钮颜色
                button.config(bg="orange")

    # 按钮点击事件处理
    def on_button_click(self, text):
        if text == "AC":  # 清除所有内容
            self.current_input = ""
            self.result = 0
            self.operator = None
        elif text == "+/-":  # 切换正负号
            if self.current_input:
                self.current_input = str(-float(self.current_input))
        elif text == "%":  # 百分比计算
            if self.current_input:
                self.current_input = str(float(self.current_input) / 100)
        elif text in ["+", "-", "*", "/"]:  # 运算符
            if self.current_input:
                self.result = float(self.current_input)
                self.operator = text
                self.current_input = ""
        elif text == "=":  # 计算
            if self.current_input and self.operator:
                self.calculate()
        else:  # 数字或小数点
            self.current_input += text

        # 更新显示内容
        self.display.delete(0, tk.END)
        self.display.insert(0, self.current_input if self.current_input else str(self.result))

    # 执行计算
    def calculate(self):
        if self.operator == "+":
            self.result += float(self.current_input)
        elif self.operator == "-":
            self.result -= float(self.current_input)
        elif self.operator == "*":
            self.result *= float(self.current_input)
        elif self.operator == "/":
            if float(self.current_input) == 0:  # 检查除数是否为零
                self.result = "错误"
            else:
                self.result /= float(self.current_input)
        self.current_input = ""
        self.operator = None

# 创建主窗口
root = tk.Tk()
calc = Calculator(root)  # 实例化计算器
root.mainloop()  # 运行主循环
